package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import data.RandomTemplate;

@SuppressWarnings("serial")
public class RandomPanel extends JPanel implements ChangeListener {
	private JLabel lblNoteMin, lblNoteMax, lblVelocityMin, lblVelocityMax;
	private JSpinner spnNoteMin, spnNoteMax, spnVelocityMin, spnVelocityMax;
	private RandomTemplate template;
	
	public RandomPanel(Color fgColor, Color bgColor, Font font, RandomTemplate template) {
		super();
		this.setBackground(bgColor);
		this.setLayout(new GridBagLayout());
		this.template = template;
		readTemplate();
		
		lblNoteMin = new JLabel("NOTE MIN: ");
		lblNoteMin.setForeground(fgColor);
		lblNoteMin.setFont(font);
		
		lblNoteMax = new JLabel("NOTE MAX: ");
		lblNoteMax.setForeground(fgColor);
		lblNoteMax.setFont(font);
		
		lblVelocityMin = new JLabel("VELOCITY MIN: ");
		lblVelocityMin.setForeground(fgColor);
		lblVelocityMin.setFont(font);
		
		lblVelocityMax = new JLabel("VELOCITY MAX: ");
		lblVelocityMax.setForeground(fgColor);
		lblVelocityMax.setFont(font);
		
		spnNoteMin = new JSpinner(new SpinnerNumberModel(template.getNotemin(), 1, 255, 1));
		spnNoteMin.setFont(font);
		spnNoteMin.addChangeListener(this);
		spnNoteMin.setValue(template.getNotemin());
		
		spnNoteMax = new JSpinner(new SpinnerNumberModel(template.getNotemax(), 1, 255, 1));
		spnNoteMax.setFont(font);
		spnNoteMax.addChangeListener(this);
		spnNoteMax.setValue(template.getNotemax());
		
		spnVelocityMin = new JSpinner(new SpinnerNumberModel(template.getVelocitymin(), 1, 255, 1));
		spnVelocityMin.setFont(font);
		spnVelocityMin.addChangeListener(this);
		spnVelocityMin.setValue(template.getVelocitymin());
		
		spnVelocityMax = new JSpinner(new SpinnerNumberModel(template.getVelocitymax(), 1, 255, 1));
		spnVelocityMax.setFont(font);
		spnVelocityMax.addChangeListener(this);
		spnVelocityMax.setValue(template.getVelocitymax());
		
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 0;
		this.add(lblNoteMin, c);
		
		c.gridx = 1;
		this.add(spnNoteMin, c);
		
		c.gridx = 0;
		c.gridy = 1;
		this.add(lblNoteMax, c);
		
		c.gridx = 1;
		this.add(spnNoteMax, c);
		
		c.gridx = 0;
		c.gridy = 2;
		this.add(lblVelocityMin, c);
		
		c.gridx = 1;
		this.add(spnVelocityMin, c);
		
		c.gridx = 0;
		c.gridy = 3;
		this.add(lblVelocityMax, c);
		
		c.gridx = 1;
		this.add(spnVelocityMax, c);
	}
	
	private void readTemplate() {
		File file = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "random");
		if (file.exists()) {
			try {
				FileInputStream fis = new FileInputStream(file);
				template.deserialize(fis);
			} 
			catch (FileNotFoundException fnfe) {
				fnfe.printStackTrace();
			}
		}
	}
	
	private void writeTemplate() {
		File file = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "random");
		try {
			FileOutputStream fos = new FileOutputStream(file);
			template.serialize(fos);
		} 
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
	}

	@Override
	public void stateChanged(ChangeEvent ce) {
		if (ce.getSource() == spnNoteMin) {
			template.setNotemin((Integer) spnNoteMin.getValue());
			writeTemplate();
		}
		else if (ce.getSource() == spnNoteMax) {
			template.setNotemax((Integer) spnNoteMax.getValue());
			writeTemplate();
		}
		else if (ce.getSource() == spnVelocityMin) {
			template.setVelocitymin((Integer) spnVelocityMin.getValue());
			writeTemplate();
		}
		else if (ce.getSource() == spnVelocityMax) {
			template.setVelocitymax((Integer) spnVelocityMax.getValue());
			writeTemplate();
		}
	}
}
